<template>
  <Modal v-model="show" width="1000" title="编辑用户">
    <UserForm
      ref="userForm_ref"
      :form-data="editItem"
      :disabled-list="disabledList"
      @on-user-submit="handleUserFormSubmit"
    ></UserForm>

    <template #footer>
      <div class="text-center mt20 mb20">
        <Button class="ivu-btn--round" @click="close">取消</Button>
        <Button class="ivu-btn--round" type="primary" @click="handleSave">保存</Button>
      </div>
    </template>
  </Modal>
</template>

<script>
import UserForm from './UserForm.vue'
// util
import { handleRequestStatus } from '@/utils/uitls.js'

// request
import { systemUserUpdate } from '@/api/system/userConfig.js'

/* 
userSource    1 门户用户 2本系统用户
若大数据平台同步过来的账号，只能对其角色进行修改；
若是新增的账号，除了初始密码字段不可修改，其他字段都可修改。
*/
export default {
  name: 'ModaEditUser',

  props: {
    visible: Boolean,
    editItem: {
      type: Object,
      default() {
        return {}
      },
    },
  },

  components: { UserForm },

  computed: {
    show: {
      get() {
        return this.visible
      },
      set(v) {
        this.$emit('update:visible', v)
      },
    },

    disabledList() {
      const { userSource } = this.editItem
      if (String(userSource) === '2') {
        return { pwd: true }
      } else {
        return {
          userName: true,
          userAccount: true,
          organId: true,
          pwd: true,
        }
      }
    },
  },

  methods: {
    close() {
      this.show = false
    },

    handleSave() {
      this.$refs['userForm_ref'].handleFormSubmit()
    },

    handleUserFormSubmit(data) {
      // console.log('handleUserFormSubmit', data)
      this.systemUserUpdate({ ...data, userId: this.editItem.userId })
    },

    async systemUserUpdate(params) {
      const res = await systemUserUpdate(params)
      handleRequestStatus(res, '编辑', () => {
        this.$emit('on-add-success')
        this.close()
      })
    },
  },
}
</script>

<style lang="less" scoped></style>
